--- 模块功能：自动注册任务.
-- @author zhangFenghe
-- @module Tweeter.registerAutoTask
-- @license MIT
-- @copyright zhangFenghe
-- @release 2022.12.06
module(..., package.seeall) -- 使得文件中的函数在何处都可调用

require "sys"
require "nvm"
require "log"
require "registerTask"

nvm.init("config.lua", true)

local strLen = string.len
local regCenter, ip, license, owner, type, groupId, clientId, uname, pwd, scope, mqtthost, topic, eqrTopic, answerTopic, port =
    nvm.get("regCenter"), nvm.get("centerConfig")[1], nvm.get("centerConfig")[2], nvm.get("centerConfig")[3],
    nvm.get("centerConfig")[4], nvm.get("centerConfig")[5],
    nvm.get("centerConfig")[6], nvm.get("centerConfig")[7], nvm.get("centerConfig")[8], nvm.get("centerConfig")[9],
    nvm.get("centerConfig")[10], nvm.get("centerConfig")[11], nvm.get("centerConfig")[12], nvm.get("centerConfig")[13],
    nvm.get("centerConfig")[14]

local function autoRegister()
    log.info("自动注册任务！！")
    log.info("regCenter", regCenter, "clientId", clientId)
    if regCenter == true then
        if clientId == nil or clientId == "" then
            if strLen(ip) > 0 and strLen(license) > 0 and strLen(owner) > 0 and strLen(type) > 0 then
                log.info("注册第二部", "ip" ~= ip and "license" ~= license and "owner" ~= owner and "type" ~= type)
                if "ip" ~= ip and "license" ~= license and "owner" ~= owner and "type" ~= type then
                    log.info("自动注册请求")
                    registerTask.RegistrationServiceTask()
                end
            end
        elseif strLen(clientId) > 0 and strLen(groupId) > 0 and strLen(uname) > 0 and strLen(pwd) > 0 then
            if (answerTopic == nil or answerTopic == "") and strLen(scope) > 0 then
                registerTask.AuthorizationApplicationUpdateServiceTask()
            end
        end
    end
end

sys.taskInit(autoRegister)
